package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.common.DeleteRequest;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.exception.ThrowUtils;
import com.yupi.springbootinit.mapper.UserMapper;
import com.yupi.springbootinit.model.dto.chart.ChartAddRequest;
import com.yupi.springbootinit.model.dto.chart.ChartUpdateRequest;
import com.yupi.springbootinit.model.entity.Chart;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.service.ChartService;
import com.yupi.springbootinit.mapper.ChartMapper;
import com.yupi.springbootinit.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
* @author liu
* @description 针对表【chart(图表信息表)】的数据库操作Service实现
* @createDate 2024-12-09 09:15:46
*/
@Service
public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
    implements ChartService{

    @Resource
    private ChartMapper chartMapper;
    @Resource
    private UserService userService;

    @Override
    public long saveChart(ChartAddRequest chartAddRequest, HttpServletRequest request) {
        Chart chart = new Chart();
        BeanUtils.copyProperties(chartAddRequest, chart);
        User loginUser = userService.getLoginUser(request);
        chart.setUserId(loginUser.getId());
        int insert = chartMapper.insert(chart);
        ThrowUtils.throwIf(insert<=0, ErrorCode.PARAMS_ERROR);
        return chart.getId();

    }

    @Override
    public long deleteChart(DeleteRequest deleteRequest, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        long id = deleteRequest.getId();
        // 判断是否存在
        Chart oldChart = chartMapper.selectById(deleteRequest.getId());
        ThrowUtils.throwIf(oldChart == null, ErrorCode.NOT_FOUND_ERROR);
        // 仅本人或管理员可删除
        if (!oldChart.getUserId().equals(user.getId()) && !userService.isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        int i = chartMapper.deleteById(deleteRequest.getId());
        ThrowUtils.throwIf(i <= 0, ErrorCode.PARAMS_ERROR);
        return i;
    }

    @Override
    public int updateChart(ChartUpdateRequest chartUpdateRequest) {
        Chart chart = new Chart();
        BeanUtils.copyProperties(chartUpdateRequest, chart);
        long id = chartUpdateRequest.getId();
        // 判断是否存在
        //Chart oldChart = chartService.getById(id);
        Chart oldChart = chartMapper.selectById(chart.getId());
        ThrowUtils.throwIf(oldChart == null, ErrorCode.NOT_FOUND_ERROR);
        //boolean result = chartService.updateById(chart);
        int i = chartMapper.updateById(chart);
        ThrowUtils.throwIf(i <= 0, ErrorCode.PARAMS_ERROR);
        return i;
    }
}




